﻿//-----------------------------------------------------------------------
// <copyright file="BaseLogModel.cs" company="SLWY Enterprises">
// * Copyright (C) 2017 四川商旅无忧科技有限公司 版权所有。
// * version : 1.0
// * author  : qinchaoyue
// * FileName: BaseLogModel.cs
// * history : created by qinchaoyue 2017-11-27 04:21:10
// </copyright>
//-----------------------------------------------------------------------


namespace Slwy.Logger.Models
{
    using System;
    using static System.TimeZone;
    using System.Net;

    using Slwy.Context;
    using Slwy.Utility.ConfigurManager;
    using Slwy.Utility.DebugLog;
    using Slwy.Utility.Helper;

    public class BaseLogModel
    {
        /// <summary>
        /// 日志id
        /// </summary>
        public Guid Id => Guid.NewGuid();

        /// <summary>
        /// 时间点
        /// 发起请求时间点
        /// </summary>
        public string RequestTime => DateTime.Now.ToString("yyyy年MM月dd日 hh:mm:ss");

        /// <summary>
        /// 时间戳
        /// </summary>
        public long Timestamp =>
            (long)(DateTime.Now - CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1))).TotalMilliseconds;

        /// <summary>
        /// 日志跟踪id
        /// 客户端请求的一个唯一ID，串联起整个调用关系
        /// </summary>
        public string TraceId
        {
            get
            {
                try
                {
                    return SlwyContext.Current[$"AutoDelete_{SlwyContext.Current.Operator.KeyId}_TraceId"].ToString();
                }
                catch (Exception e)
                {
                    Logs.Err("方法: Slwy.Logger.Models.BaseLogModel.TraceId.get, 错误: " + e);
                    return "获取TraceId失败";
                }
            }
        }

        /// <summary>
        /// 本机ip
        /// </summary>
        public string Ip
        {
            get
            {
                try
                {
                    return DnsHelper.GetIp();
                }
                catch (Exception e)
                {
                    Logs.Err("方法: Slwy.Logger.Models.BaseLogModel.Ip.get, 错误: " + e);
                    return "获取本机Ip失败";
                }
            }
        }

        /// <summary>
        /// 应用程序名
        /// 异常的服务名称
        /// </summary>
        public string ServiceName
        {
            get
            {
                try
                {
                    return new ConfigurHandler().GetAppOptionValue<string>("ServiceInfo", "Name");
                }
                catch (Exception e)
                {
                    Logs.Err("方法: Slwy.Logger.Models.BaseLogModel.ServiceName.get, 错误: " + e);
                    return "服务名称获取失败";
                }
            }
        }

        /// <summary>
        /// 主机名
        /// 服务所在的机器名
        /// </summary>
        public string Host
        {
            get
            {
                try
                {
                    return Dns.GetHostName();
                }
                catch (Exception e)
                {
                    Logs.Err("方法: Slwy.Logger.Models.BaseLogModel.Host.get, 错误: " + e);
                    return "获取主机名失败";
                }
            }
        }
    }
}
